<?php

namespace Respect\StackedRestfulCore;

use Respect\Environment\PhpCgiCompatibleEnv as Env;
use Respect\Http\Request;
use Respect\SimpleDataContainer\Container;
use Respect\StackedModularDispatcher\Dispatcher;
use Respect\SimpleRouter\Router;
use Respect\StackedViewRenderer\ViewRenderer;

class Core
{

    public function __construct($applicationNamespace)
    {
        $env = new Env();
        $request = new Request($env);
        $dataContainer = new Container();
        $moduleLoader = $request->getModuleLoader();
        $router = new Router($env, $applicationNamespace);
        $classes = $router->getControllerClassNames();
        $dispatcher = new Dispatcher($dataContainer, $moduleLoader);
        $dispatcher->appendInstances($classes);
        $dispatcher->run();
        $view = new ViewRenderer($dataContainer);
        $view->setMimetypeHandler($moduleLoader('Content'));
        $view->appendInstances($dispatcher->getInstances());
        $view->render();
        return $this;
    }

}